(function(window){
	function Player(audio){
		return new Player.prototype.init(audio);
	}
	Player.prototype={
		constructor:Player,
		musicList:[],
		init:function(audio){
			this.$audio=audio;		//jq对象
			this.audio=audio.get(0);		//js对象
		},
		nowindex:0,
		playmusic:function(index,music){
			if(this.nowindex==index){
				if(this.audio.paused){
					this.audio.play();
				}
				else{
					this.audio.pause();
				}
			}
			else{
				this.$audio.attr("src",music.link_url);
				this.audio.play();
				this.nowindex=index;
			}
		},
		preindex:function(){
			if($(".music_mode").attr("class").indexOf("mode2")!=-1){
				var index=Math.round((Math.random()*this.musicList.length))
			}
			else{
				var index=this.nowindex-1
				if(index<0){
					index=this.musicList.length-1
				}
			}
			return index
		},
		nextindex:function(){
			if($(".music_mode").attr("class").indexOf("mode2")!=-1){
				var index=Math.round((Math.random()*this.musicList.length))
			}
			else{
				var index=this.nowindex+1
				if(index>this.musicList.length-1){
					index=0
				}
			}
			return index
		},
		delmusic:function(index){
			this.musicList.splice(index,1)
			if(index<this.nowindex){
				this.nowindex-=1
			}
		},

		time_update:function(back){
			this_player=this
			this.$audio.on("timeupdate",function(){
				var all_time=this.duration
				var now_time=this.currentTime
				var format_time=this_player.formattime(all_time,now_time)
				back(all_time,now_time)
			})
		},
		
		formattime:function (all_time,now_time){
			var end_minute=parseInt(all_time/60)
			var end_second=parseInt(all_time%60)
			if(end_minute<10){
				end_minute="0"+end_minute
			}
			if(end_second<10){
				end_second="0"+end_second
			}
			var now_minute=parseInt(now_time/60)
			var now_second=parseInt(now_time%60)
			if(now_minute<10){
				now_minute="0"+now_minute
			}
			if(now_second<10){
				now_second="0"+now_second
			}
			var time=now_minute+":"+now_second+"/"+end_minute+":"+end_second
			if(now_time>0){
				$(".jindu_time").text(time)
			}
		},
		
		music_skip:function(rate){
			this.audio.currentTime=this.audio.duration*rate
		},
		voice_click:function(rate){
			this.audio.volume=rate
		}
	}
	Player.prototype.init.prototype=Player.prototype;
	window.Player=Player;
})(window);
